import { Model, Effect } from "dva-core-ts";
import { Reducer } from "redux";
import {
  AUTH_ENUM,
  LOGIN_ENUM,
  resetSignupAction,
  signinFail,
  signinSuccess,
  signupFail,
  signupSuccess,
} from "./action";
import { signinService, signupService } from "./service";
// import { AxiosError } from "axios";
export interface AuthState {
  signup: {
    loaded: boolean;
    success: boolean;
    message?: string;
  };
  signin: {
    loaded: boolean;
    success: boolean;
    message?: string;
  };
}
const handleSignup = "handleSignup";
const resetSignup = "resetSignup";
const handleSignin = "handleSignin";
interface AuthModel extends Model {
  namespace: "auth";
  state: AuthState;
  reducers: {
    setState: Reducer<AuthState>;
  };
  effects: {
    [handleSignup]: Effect;
    [resetSignup]: Effect;
    [handleSignin]: Effect;
  };
}
const initialState: AuthState = {
  signup: {
    loaded: false,
    success: false,
    message: "",
  },
  signin: {
    loaded: false,
    success: false,
    message: "",
  },
};

export const AUTH_EFFECT = {
  handleSignup: `auth/` + handleSignup,
  resetSignup: `auth/` + resetSignup,
  handleSignin: `auth/` + handleSignin,
};
const authModel: AuthModel = {
  namespace: "auth",
  state: initialState,
  reducers: {
    setState(state = initialState, { payload }) {
      switch (payload.type) {
        case AUTH_ENUM.SIGNUP:
          return {
            ...state,
            signup: {
              loaded: false,
              success: false,
              message: "",
            },
          };
        case AUTH_ENUM.SIGNUP_SUCCESS:
          return {
            ...state,
            signup: {
              loaded: true,
              success: true,
              message: "",
            },
          };
        case AUTH_ENUM.SIGNUP_FAIL:
          return {
            ...state,
            signup: {
              loaded: true,
              success: false,
              message: payload.message,
            },
          };
        case AUTH_ENUM.RESET_SIGNUP:
          return {
            ...state,
            signup: {
              loaded: false,
              success: false,
              message: "",
            },
          };
        case LOGIN_ENUM.SIGNIN:
          return {
            ...state,
            signin: {
              loaded: false,
              success: false,
              message: "",
            },
          };
        case LOGIN_ENUM.SIGNIN_SUCCESS:
          return {
            ...state,
            signin: {
              loaded: true,
              success: true,
              message: "",
            },
          };
        case LOGIN_ENUM.SIGNIN_FAIL:
          return {
            ...state,
            signin: {
              loaded: true,
              success: false,
              message: payload.message,
            },
          };
        default:
          return {
            ...state,
          };
      }
    },
  },
  effects: {
    *[handleSignup]({ payload }, { call, put }) {
      let res;
      try {
        res = yield call(signupService, payload);
        yield put({
          type: "setState",
          payload: signupSuccess(),
        });
      } catch (error) {
        console.log(error);
        res = error?.response?.data?.error;
        yield put({
          type: "setState",
          payload: signupFail(error?.response?.data?.error),
        });
      } finally {
        console.log(111);
        return res;
      }
    },
    *[resetSignup]({ payload }, { call, put }) {
      yield put({
        type: "setState",
        payload: resetSignupAction(),
      });
    },
    *[handleSignin]({ payload }, { call, put }) {
      let res;
      try {
        res = yield call(signinService, payload);
        localStorage.setItem("jwt", JSON.stringify(res.data));
        yield put({
          type: "setState",
          payload: signinSuccess(),
        });
      } catch (error) {
        console.log(error);
        res = error?.response?.data?.error;
        yield put({
          type: "setState",
          payload: signinFail(error?.response?.data?.error),
        });
      } finally {
        return res;
      }
    },
  },
};
export default authModel;
